cmake_minimum_required(VERSION 2.6)
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)

project(pio)

set(Libev_Root $ENV{LIBEVENT_ROOT})
if (Libev_Root)
	set(Libev_Inc ${Libev_Root}/include)
	set(Libev_Lib ${Libev_Root}/lib)
elseif()
	message(FATAL_ERROR "LIBEVENT_ROOT not found")
endif()

option(64BIT "64BIT" FALSE)
if (64BIT)
	add_definitions(-D__64BIT__)
endif()

option(WINDOWS "WINDOWS" FALSE)
if (WINDOWS)
	add_definitions(-D__WINDOWS__)
	add_definitions(-D_WIN32_WINNT=0x0501)
	add_definitions(-W3)
else ()
	add_definitions(-D__LINUX__)
	add_definitions(-Wall)
endif()

option(DEBUG "DEBUG" FALSE)
if (DEBUG)
add_definitions(-D__DEBUG__)
endif()

option(ECHO "BUILD ECHO SERVER" FALSE)

set(pio_SOURCES
	pio/server.h
	src/server.c
)

set(tstl2cl_SOURCES
	tstl2cl/c_def.h
	tstl2cl/c_function.h
	tstl2cl/c_function.c
	tstl2cl/c_iterator.h
	tstl2cl/c_iterator.c
	tstl2cl/c_map.c
	tstl2cl/c_map.h
	tstl2cl/c_memory.c
	tstl2cl/c_memory.h
	tstl2cl/c_pair.c
	tstl2cl/c_pair.h
	tstl2cl/c_tree.c
	tstl2cl/c_tree.h
	tstl2cl/c_algo.c
	tstl2cl/c_algo.h
)

if (ECHO)
set(echo_SOURCES
	main.c
)
elseif(WINDOWS)
set(def_SOURCES
	pio.def
)
endif()

include_directories( 
	${PROJECT_SOURCE_DIR}
	${Libev_Inc}
)
link_directories(
	${Libev_Lib}
)

if (ECHO)
	add_executable(pio ${pio_SOURCES} ${tstl2cl_SOURCES} ${echo_SOURCES})
else()
	add_library(pio SHARED ${pio_SOURCES} ${tstl2cl_SOURCES} ${def_SOURCES})
endif()

if (WINDOWS)
	target_link_libraries(pio ws2_32 libevent libevent_core libevent_extras)
else()
	target_link_libraries(pio event event_core event_extra)
endif()
